import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI, { Avatar } from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import moment from 'moment';
import store from "./store"
import * as echarts from 'echarts'
import {download,templateDownload,downloadFile} from '@/utils/file';
Vue.config.productionTip = false;
Vue.prototype.download = download;
Vue.prototype.templateDownload = templateDownload;
Vue.prototype.downloadFile = downloadFile;
Vue.prototype.$echarts = echarts

Vue.use(VueI18n);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});

NProgress.configure({     
    easing: 'ease',  // 动画方式    
    speed: 500,  // 递增进度条的速度    
    showSpinner: false, // 是否显示加载ico    
    trickleSpeed: 200, // 自动递增间隔    
    minimum: 0.3 // 初始化时的最小百分比
})


const whiteList = ['/login'] // 不重定向白名单

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {

    router.onError((error) => {
        const pattern = /Loading chunk (\d)+ failed/g;
        const isChunkLoadFailed = error.message.match(pattern);
        const targetPath = router.history.pending.fullPath;
        if (isChunkLoadFailed) {
         router.replace(targetPath);
        }
       });

     // 每次切换页面时，调用进度条
     NProgress.start();
     if (to.meta.title) {
        document.title = to.meta.title
      }
    //  const token = localStorage.getItem("token");
    const token = store.getters.token;
    //console.log("token:"+token);
     if(token){
         if(to.path === '/login'){
             next({path: '/'})
         }else{
             // 判断当前用户是否已拉取完userInfo信息
             if(store.getters.roles== null || store.getters.roles.length === 0){
                 // 拉取用户信息
                 store.dispatch('GetUserInfo').then(res=>{
                    //console.log(res);
                    if(res.code===200){
                        store.dispatch('GetMenu').then(res=>{
                            next({...to,replace: true}) 
                        })
                    }
                    // next('/')
                 })
             }else{
                next()
             }
         }
     }else{
         if(whiteList.indexOf(to.path) !== -1){
             next()
         }else{
             next({path: '/login'})
         }
     }
});

router.afterEach(() => {  
    // 在即将进入新的页面组件前，关闭掉进度条
    NProgress.done()
})
/* 路由异常错误处理，尝试解析一个异步组件时发生错误，重新渲染目标页面 */
router.onError((error) => {
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);
    const targetPath = router.history.pending.fullPath;
    if (isChunkLoadFailed) {
      router.replace(targetPath);
    }
  });
  
new Vue({
    router,
    i18n,
    store,
    render: h => h(App)
}).$mount('#app');
